﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class MailServerDAO
    {
        public static List<MailServerDTO> getAll()
        {
            List<MailServerDTO> result = new List<MailServerDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.MAILSERVERs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                MailServerDTO dto = new MailServerDTO();

                dto.MaMailServer = ds.MaMailServer;
                dto.DomainName = ds.DomainName;
                dto.MailServer = ds.MailServer1;
                dto.UserName = ds.UserName;
                dto.Password = ds.Password;
                dto.Port = ds.Port.Value;
                dto.SecuredAuthentication = ds.SecuredAuthentication;
                dto.MaTreatAs = ds.MaTreatAs.Value;

                result.Add(dto);
            }

            return result;
        }

        public static MailServerDTO getAlongCode(int maMail)
        {
            MailServerDTO result = new MailServerDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.MAILSERVERs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaMailServer == maMail).Single();

            result.MaMailServer = query.MaMailServer;
            result.DomainName = query.DomainName;
            result.MailServer = query.MailServer1;
            result.UserName = query.UserName;
            result.Password = query.Password;
            result.Port = query.Port.Value;
            result.SecuredAuthentication = query.SecuredAuthentication;
            result.MaTreatAs = query.MaTreatAs.Value;

            return result;
        }

        public static void Insert(MailServerDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            MAILSERVER mail = new MAILSERVER();            
            mail.DomainName = _insert.DomainName;
            mail.MailServer1 = _insert.MailServer;
            mail.UserName = _insert.UserName;
            mail.Password = _insert.Password;
            mail.Port = _insert.Port;
            mail.SecuredAuthentication = _insert.SecuredAuthentication;
            mail.MaTreatAs = _insert.MaTreatAs;
            mail.Deleted = _insert.Deleted;

            dbRaoVat.MAILSERVERs.InsertOnSubmit(mail);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maMail)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            MAILSERVER _delete = (from n in dbRaoVat.MAILSERVERs
                                 where n.MaMailServer == maMail && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(MailServerDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            MAILSERVER _update = (from n in dbRaoVat.MAILSERVERs
                                  where n.MaMailServer == dto.MaMailServer && n.Deleted == 0
                                  select n).Single();

            _update.DomainName = dto.DomainName;
            _update.MailServer1 = dto.MailServer;
            _update.UserName = dto.UserName;
            _update.Password = dto.Password;
            _update.Port = dto.Port;
            _update.SecuredAuthentication = dto.SecuredAuthentication;
            _update.MaTreatAs = dto.MaTreatAs;            

            dbRaoVat.SubmitChanges();
        }
    }
}
